gapminder
## # A tibble: 1,704 x 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
## 7 Afghanistan Asia 1982 39.9 12881816 978.
## 8 Afghanistan Asia 1987 40.8 13867957 852.
## 9 Afghanistan Asia 1992 41.7 16317921 649.
## 10 Afghanistan Asia 1997 41.8 22227415 635.
## # … with 1,694 more rows
p <- ggplot(data = gapminder)
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap,
y = lifeExp))
p
This empty plot has no geoms.
p + geom_point()
A scatterplot of Life Expectancy vs GDP
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap,
y=lifeExp))
p + geom_smooth()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
Life Expectancy vs GDP, using a smoother.
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap,
y=lifeExp))
p + geom_point() + geom_smooth()
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
Life Expectancy vs GDP, showing both points and a GAM smoother.
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap,
y=lifeExp))
p + geom_point() + geom_smooth(method = "lm")
## `geom_smooth()` using formula 'y ~ x'
Life Expectancy vs GDP, points and an ill-advised linear fit.
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap,
y=lifeExp))
p + geom_point() +
geom_smooth(method = "gam") +
scale_x_log10()
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'
Life Expectancy vs GDP scatterplot, with a GAM smoother and a log scale on the x-axis.
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y=lifeExp))
p + geom_point() +
geom_smooth(method = "gam") +
scale_x_log10(labels = scales::dollar)
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'
Life Expectancy vs GDP scatterplot, with a GAM smoother and a log scale on the x-axis, with better labels on the tick marks.
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap,
y = lifeExp,
color = "purple"))
p + geom_point() +
geom_smooth(method = "loess") +
scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'
What has gone wrong here?
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap,
y = lifeExp))
p + geom_point(color = "purple") +
geom_smooth(method = "loess") +
scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'
Setting the color attribute of the points directly.
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap,
y = lifeExp))
p + geom_point(alpha = 0.3) +
geom_smooth(color = "orange", se = FALSE, size = 8, method = "lm") +
scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'
Setting some other arguments.
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y=lifeExp))
p + geom_point(alpha = 0.3) + geom_smooth(method = "gam") +
scale_x_log10(labels = scales::dollar) +
labs(x = "GDP Per Capita", y = "Life Expectancy in Years",
title = "Economic Growth and Life Expectancy",
subtitle = "Data points are country-years",
caption = "Source: Gapminder.")
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'
A more polished plot of Life Expectancy vs GDP.
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap,
y = lifeExp,
color = continent))
p + geom_point() +
geom_smooth(method = "loess") +
scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'
Mapping the continent variable to the color aesthetic.
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap,
y = lifeExp,
color = continent,
fill = continent))
p + geom_point() +
geom_smooth(method = "loess") +
scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'
Mapping the continent variable to the color aesthetic, and correcting the error bars using the fill aesthetic.
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp))
p + geom_point(mapping = aes(color = continent)) +
geom_smooth(method = "loess") +
scale_x_log10()
## `geom_smooth()` using formula 'y ~ x'
Mapping aesthetics on a per-geom basis. Here color is mapped to continent for the points but not the smoother.
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap,
y = lifeExp))
p + geom_point(mapping = aes(color = log(pop))) +
scale_x_log10()
Mapping a continuous variable to color.
knitr::opts_chunk$set(fig.width=8, fig.height=5)
ggsave(filename = "figures/my_figure.png")
here()
## [1] "/Users/zhuhadar/R-workspace/Lily R Data Vis/LilyFork_kjhealy/stathorizons_0820"
p_out <- p + geom_point(mapping = aes(color = log(pop))) +
scale_x_log10()
ggsave(here("figures", "lifexp_vs_gdp_gradient.pdf"), plot = p_out)
## Saving 7 x 5 in image
ggsave(here("figures", "lifexp_vs_gdp_gradient.png"), plot = p_out)
## Saving 7 x 5 in image